from django.db import models

from apps.users.models import BaseModel


# 所在城市由于城市比较多，所以新建一个表用于保存城市信息方便后期添加删除城市信息；
class City(BaseModel):
    name = models.CharField(max_length=20, verbose_name=u'城市名')
    desc = models.CharField(max_length=200, verbose_name=u'描述')

    class Meta:
        verbose_name = '城市'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class CourseOrg(BaseModel):
    name = models.CharField(max_length=50, verbose_name='机构名称 ')
    desc = models.TextField(verbose_name='描述')
    tag = models.CharField(default='全国知名', max_length=10, verbose_name='机构标签')
    category = models.CharField(default='pxjg', verbose_name='机构类别', max_length=4,
                                choices=(('pxjg', '培训机构'), ('gr', '个人'), ('gx', '高校')))
    click_nums = models.IntegerField(default=0, verbose_name='点击数')
    students = models.IntegerField(default=0, verbose_name='学习人数')
    fav_nums = models.IntegerField(default=0, verbose_name='收藏数')
    course_nums = models.IntegerField(default=0, verbose_name='课程数')
    address = models.CharField(max_length=150, verbose_name='机构地址')
    image = models.ImageField(upload_to='org/%Y/%m', verbose_name=u'logo', max_length=100)
    city = models.ForeignKey(City, verbose_name='所在城市', on_delete=models.CASCADE)

    is_auth = models.BooleanField(default=False, verbose_name="是否认证")
    is_gold = models.BooleanField(default=False, verbose_name="是否金牌")

    # 自定义方法取得 该课程机构 下 讲师 的 课程
    def courses(self):  # self 指向的是 CourseOrg 的实例
        # 找出是经典的课程，并截取前三个课程给前端
        courses = self.course_set.filter(is_classics=True)[:3]  # course_set 指向的是 Course 课程
        return courses

    class Meta:
        verbose_name = '课程机构'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Teacher(BaseModel):
    org = models.ForeignKey(CourseOrg, on_delete=models.CASCADE, verbose_name='所属机构')
    name = models.CharField(verbose_name='教师名', max_length=50)
    work_years = models.IntegerField(default=0, verbose_name='工作年限')
    work_company = models.CharField(verbose_name='就职公司', max_length=50)
    work_position = models.CharField(verbose_name='公司职位', max_length=50)
    points = models.CharField(verbose_name='教学特点', max_length=50)
    click_nums = models.IntegerField(default=0, verbose_name='点击数')
    fav_nums = models.IntegerField(default=0, verbose_name='收藏数')
    age = models.IntegerField(default=18, verbose_name='年龄')
    image = models.ImageField(upload_to='teacher/%Y/%m', verbose_name='头像', max_length=100)

    class Meta:
        verbose_name = '教师'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    # 获取该教师的所有课程数
    def course_nums(self):
        return self.course_set.all().count()
